import { Injectable } from '@angular/core';
import { environment } from '../../environments/environment';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { EntityPageResult } from '../model/entity-page-result';
import { ReplyCollectMemberVo } from '../model/reply-collect-member-vo';
import { ReplyMoodVo } from '../model/reply-mood-vo';
import { ThreadsVo } from '../model/threads-vo';
import { EMPTY, throwError, Observable } from 'rxjs';
import { ReplyVo } from '../model/reply-vo';
@Injectable({
  providedIn: 'root'
})
export class ReplyService {
  private restRequestURI: string = environment.restURI + '/reply';
  /**
   * 回复REST API服务
   */
  constructor(private http: HttpClient) { }

  /**
   * 查看指定的回复
   * @param postId 回复ID
   */
  public get(postId: number): Observable<ReplyVo> {
    if (0 >= postId) {
      return throwError('回复ID非可用值');
    }
    //:/{id}
    return this.http.get<ReplyVo>(this.restRequestURI + '/' + postId);
  }
  /**
   * 分页查看指定话题下的回复
   * @param topicId 话题ID
   * @param filterMember 过滤的话题作者/会员ID
   * @param scale 图片的尺寸,若为null表示自动
   * @param page 页码
   * @param pageSize 每页显示的数量
   * @param sortCategory 0升序,1降底,其它值都是升序
   */
  public getAll(topicId: number, filterMember: number = 0, scale: string = '640x360', page: number = 1, pageSize: number = 20, sortCategory: number=0): Observable<EntityPageResult<ReplyCollectMemberVo>> {
    //:/threads/{topic}/{filterMember}/{scale}/{page}/{number}
    if (0 >= topicId) {
      return throwError('话题ID非可用值');
    }
    let params = new HttpParams().append('sort', sortCategory + '');
    return this.http.get<EntityPageResult<ReplyCollectMemberVo>>(this.restRequestURI + '/threads/' + topicId + '/' + filterMember + '/' + scale + '/' + page + '/' + pageSize, { params: params });
  }
  /**
   * 查看指定回复的喜好记录
   * 
   * @param topicId 话题ID
   * @param postsId 回复ID
   */
  public getMoodRecord(topicId: number, postsId: number): Observable<ReplyMoodVo> {
    //:/threads/{topic}/{id}/mood
    if (0 >= postsId) {
      return throwError('回复ID非可用值');
    }
    if (0 >= topicId) {
      return throwError('话题ID非可用值');
    }
    return this.http.get<ReplyMoodVo>(this.restRequestURI + '/threads/' + topicId + '/' + postsId + '/mood');
  }
  /**
   * 查看指定回复的喜好记录
   * 
   * @param topicId 话题ID
   * @param postsIds 回复ID数组,格式:id1,id2,...idN
   */
  public getMultiMoodRecords(topicId: number, postsIds: number[]): Observable<ReplyMoodVo[]>{
    if (0 >= topicId) {
      return throwError('话题ID非可用值');
    }
    if(!postsIds || postsIds.length == 0){
      return throwError('缺少回复ID');
    }
    //:/mood/query
    const httpOptions = {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    };
    const body = new HttpParams().set('threads', topicId+'').set('records', postsIds.join());
    return this.http.post<ReplyMoodVo[]>(this.restRequestURI + '/mood/query', body.toString(), httpOptions);
  }
  /**
   * 添加喜好记录
   * @param postsId 回复ID
   * @param topicId 话题ID
   * @param moodStatus 喜好值(1表示喜欢,2表示讨厌)
   * @param memberId 会员ID
   * @param memberNickname 会员昵称
   * @param token 
   * @param ipAddr 
   * @returns 成功的状态码是200(响应的内容中ADD表示增加成功,DEL表示删除成功),失败的状态是417
   */
  public toggleMoode(postsId: number, topicId: number, moodStatus: number, memberId: number, memberNickname: string, token: string, ipAddr: string): Observable<string> {
    if (0 >= postsId) {
      return throwError('回复ID非可用值');
    }
    if (0 >= topicId) {
      return throwError('话题ID非可用值');
    }
    if (1 !== moodStatus && 2 !== moodStatus) {
      return throwError('喜好值非可用值');
    }
    if (0 >= memberId) {
      return throwError('暂不支持游客操作');
    }
    //:/{id}/mood
    const headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
    const body = new HttpParams().set('threads', topicId + '').set('mood', moodStatus + '').set('member', memberId + '').set('nickname', memberNickname).set('token', token).set('ip', ipAddr);
    return this.http.post(this.restRequestURI + '/' + postsId + '/mood', body.toString(), { headers, "responseType": "text" });//状态码
  }
  /**
   * 举报回复
   * @param postsId 回复ID
   * @param topicId 话题ID
   * @param reportContent 举报的内容
   * @param reportType 举报的类型
   * @param memberId 会员ID
   * @param memberNickname 会员昵称
   * @param token 
   * @param ipAddr 
   * @returns 成功的状态码是200(响应的内容是ThreadsVo),失败的状态:404(表示创建失败),400(前置检查时触发的响应)
   */
  public report(postsId: number, topicId: number, reportContent: string, reportType: number, memberId: number, memberNickname: string, token: string, ipAddr: string): Observable<ThreadsVo> {
    if (0 >= postsId) {
      return throwError('回复ID非可用值');
    }
    if (0 >= topicId) {
      return throwError('话题ID非可用值');
    }
    if (!reportContent || reportContent.length == 0) {
      return throwError('举报内容非可用值');
    }
    if (0 >= reportType) {
      return throwError('举报类型非可用值');
    }
    if (0 >= memberId) {
      return throwError('暂不支持游客举报回复');
    }
    //:/{id}/report
    const httpOptions = {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    };
    const body = new HttpParams().set('threads', topicId + '').set('content', reportContent).set('type', reportType + '').set('member', memberId + '').set('nickname', memberNickname).set('token', token).set('ip', ipAddr);
    return this.http.post<ThreadsVo>(this.restRequestURI + '/' + postsId + '/report', body.toString(), httpOptions);
  }
  /**
   * 回复话题
   * @param boardGroupId 版块组ID
   * @param boardId 版块ID
   * @param topicId 话题ID
   * @param quotePostsId 引用的回复ID,若未引用使用0
   * @param content 回复的内容
   * @param scale 图片的尺寸,若为null表示自动.在引用回复时使用
   * @param memberId 会员ID
   * @param memberNickname 会员昵称
   * @param token 
   * @param ipAddr 
   * @returns 成功返回200(响应的内容是ReplyVo),失败404(没有具体的错误描述)
   */
  public reply(
    boardGroupId: number,
    boardId: number,
    topicId: number,
    quotePostsId: number = 0,
    content: string,
    scale: string = '640x360',
    memberId: number,
    memberNickname: string,
    token: string,
    ipAddr: string): Observable<ReplyVo> {
    if (0 >= boardGroupId) {
      return throwError('版块组ID非可用值');
    }
    if (0 >= boardId) {
      return throwError('版块ID非可用值');
    }
    if (0 >= topicId) {
      return throwError('话题ID非可用值');
    }
    if (!content || content.length == 0) {
      return throwError('回复内容非可用值');
    }
    if (0 >= memberId) {
      return throwError('暂不支持游客回复话题');
    }
    //:/create
    const httpOptions = {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    };
    const body = new HttpParams()
      .set('volumes', boardGroupId + '')
      .set('board', boardId + '')
      .set('threads', topicId + '')
      .set('quote', quotePostsId + '')
      .set('content', content)
      .set('scale', scale)
      .set('member', memberId + '')
      .set('nickname', memberNickname)
      .set('token', token)
      .set('ip', ipAddr);
    return this.http.post<ReplyVo>(this.restRequestURI + '/create', body.toString(), httpOptions);
  }
}
